package com.exam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.ApiResult;
import com.exam.entity.Shi;
import com.exam.entity.Zhi;
import com.exam.serviceimpl.ShiServiceImpl;
import com.exam.serviceimpl.ZhiServiceImpl;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ShiController {

    @Autowired
    private ShiServiceImpl examManageService;

    @GetMapping("/Shi")
    public ApiResult findAll(){
        System.out.println("不分页查询所有试卷");
        ApiResult apiResult;
        apiResult = ApiResultHandler.buildApiResult(200, "请求成功！", examManageService.findAll());
        return apiResult;
    }

    @GetMapping("/Shi/{page}/{size}")
    public ApiResult findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        System.out.println("分页查询所有试卷");
        ApiResult apiResult;
        Page<Shi> examManage = new Page<>(page,size);
        IPage<Shi> all = examManageService.findAll(examManage);
        apiResult = ApiResultHandler.buildApiResult(200, "请求成功！", all);
        return apiResult;
    }
    @GetMapping("/Shi/{page}/{size}/{subject}/{course}")
    public ApiResult findBySub(@PathVariable("page") Integer page, @PathVariable("size") Integer size,@PathVariable("subject") String subject,@PathVariable("course") Integer course){
        System.out.println("分页查询所有试卷");
        ApiResult apiResult;
        Page<Shi> examManage = new Page<>(page,size);
        IPage<Shi> all = examManageService.findBySub(examManage,subject,course);
        apiResult = ApiResultHandler.buildApiResult(200, "请求成功！", all);
        return apiResult;
    }
    @DeleteMapping("/Shi/{id}")
    public ApiResult deleteById(@PathVariable("id") Integer examCode){
        int res = examManageService.delete(examCode);
        return ApiResultHandler.buildApiResult(200,"删除成功",res);
    }
//
//    @GetMapping("/exam/{examCode}")
//    public ApiResult findById(@PathVariable("examCode") Integer examCode){
//        System.out.println("根据ID查找");
//        ExamManage res = examManageService.findById(examCode);
//        if(res == null) {
//            return ApiResultHandler.buildApiResult(10000,"考试编号不存在",null);
//        }
//        return ApiResultHandler.buildApiResult(200,"请求成功！",res);
//    }
//
//    @DeleteMapping("/exam/{examCode}")
//    public ApiResult deleteById(@PathVariable("examCode") Integer examCode){
//        int res = examManageService.delete(examCode);
//        return ApiResultHandler.buildApiResult(200,"删除成功",res);
//    }
//
    @PutMapping("/Shi")
    public ApiResult update(@RequestBody Shi exammanage){
        int res = examManageService.update(exammanage);
//        if (res == 0) {
//            return ApiResultHandler.buildApiResult(20000,"请求参数错误");
//        }
        System.out.print("更新操作执行---");
        return ApiResultHandler.buildApiResult(200,"更新成功",res);
    }
//
    @PostMapping("/Shi")
    public ApiResult add(@RequestBody Shi exammanage){
        int res = examManageService.add(exammanage);
        if (res ==1) {
            return ApiResultHandler.buildApiResult(200, "添加成功", res);
        } else {
            return  ApiResultHandler.buildApiResult(400,"添加失败",res);
        }
    }
//    //根据题目数量计算总分
//    @PostMapping("/exam")
//    public ApiResult add1(@RequestBody examInfoDTO exammanage){
//        int num1 = exammanage.getInfo().getNum1();
//        int num2 = exammanage.getInfo().getNum2();
//        int num3 = exammanage.getInfo().getNum3();
//        int sum = num1*2+num2*3+num3*5;
//         ExamManage form = exammanage.getForm();
//         form.setTotalScore(sum);
//         testInfo x = exammanage.getInfo();
////        int res = examManageService.add(form);
//        System.out.println(exammanage.getInfo());
//        return null;
////        if (res ==1) {
////            return ApiResultHandler.buildApiResult(200, "添加成功", res);
////        } else {
////            return  ApiResultHandler.buildApiResult(400,"添加失败",res);
////        }
//    }
//
//    @GetMapping("/examManagePaperId")
//    public ApiResult findOnlyPaperId() {
//        ExamManage res = examManageService.findOnlyPaperId();
//        if (res != null) {
//            return ApiResultHandler.buildApiResult(200,"请求成功",res);
//        }
//        return ApiResultHandler.buildApiResult(400,"请求失败",res);
//    }
//    //设置试卷实际内容
////    @PostMapping("/exam/set")
////    public void set(@RequestBody testInfo testinfo){
////        System.out.println(testinfo);
////    }

}
